// pages/list/list.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    serverUrl: app.globalData.serverUrl,
    height: app.globalData.height,
    width: app.globalData.width,
    barHeight: app.globalData.barHeight,
    input: '',
    tag: 1,
    exItems: [],
    hpItems: [],
    slItems: [],
    trItems: [],
    flag: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  input: function (e) {
    this.setData({
      input: e.detail.value
    })
  },
  search: function (e) {
    if (this.data.input == '') {

    } else {
      wx.navigateTo({
        url: '/pages/search/search?search=' + this.data.input,
        complete: res => {
          this.setData({
            input: '',
          })
        }
      })
    }
  },
  select: function (e) {
    this.setData({
      tag: e.currentTarget.dataset.tag,
    })
  },
  //获取个人订单
  getList: function () {
    var that = this;
    try {
      const token = wx.getStorageSync('token');
      if (token) {
        wx.request({
          url: app.globalData.serverUrl + '/admin/me/getlist',
          method: 'POST',
          data: {
            token: token,
          },
          success(res) {
            var exarr = res.data.list.express;
            var hparr = res.data.list.help;
            var slarr = res.data.list.sale;
            var trarr = res.data.list.teacher;

            that.getPic(exarr, 1);
            that.getPic(hparr, 2);
            that.getPic(slarr, 3);
            that.getPic(trarr, 4);

            var arr = exarr;
            arr = that.merge(arr, hparr)
            arr = that.merge(arr, slarr)
            arr = that.merge(arr, trarr)

            that.setData({
              exItems: exarr,
              hpItems: hparr,
              slItems: slarr,
              trItems: trarr,
              allItems: arr,
            })

          }
        });
      }
    } catch (e) {
      wx.showToast({
        title: '获取错误，请重新获取！',
        icon: 'success',
        duration: 2000
      });
    }

  },
  merge: function (arr1, arr2) {
    var temp = [];
    var len1 = arr1.length;
    var len2 = arr2.length;
    var n = 0;
    var m = 0;
    while (n < len1 && m < len2) {
      temp.push(arr1[n]['create_time'] > arr2[m]['create_time'] ? arr1[n++] : arr2[m++])
    }
    while (n < len1 || m < len2) {
      n >= len1 ? null : temp.push(arr1[n++]);
      m >= len2 ? null : temp.push(arr2[m++]);
    }
    return temp;
  },

  //取出图片
  getPic: function (arr, n) {
    let length = arr.length;
    for (var i = 0; i < length; i++) {
      try {
        arr[i].type = n;
        arr[i].create_time = (new Date(arr[i]['create_time'].replace(/-/g, "/"))).valueOf();
        arr[i].paths = arr[i].paths.split(',');
      } catch (e) {

      }
    };
  },
  getDetail: function (e) {
    let item = e.currentTarget.dataset.item;
    item = JSON.stringify(item);
    var address = '';
    let type = e.currentTarget.dataset.type;
    switch (type.toString()) {
      case '1':
        address = 'expressDetail';
        break;
      case '2':
        address = 'helpDetail';
        break;
      case '3':
        address = 'saleDetail';
        break;
      case '4':
        address = 'teacherDetail';
        break;
    }
    wx.navigateTo({
      url: '/pages/' + address + '/' + address + '?item=' + item
    })
  },
})